import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author lys
 * 个人信息页面
 */

public class Profile {
    private static Profile instance;

    public JLayeredPane  jLayeredPane = new JLayeredPane();
    public JPanel jPanel2 = new JPanel();
    // 标签
    private JLabel jLabel = new JLabel("账户管理");
    // 按钮
    private JButton button = new JButton("修改信息");
    private JButton button2 = new JButton("借书记录");
    private JButton button3 = new JButton("退出登录");
    // 字体
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font1 = new Font("宋体", Font.BOLD, 25);
    private Font font2 = new Font("宋体", Font.BOLD,40);

    String s1;
    String s2;
    String s3;
    private JLabel jLabel1 = new JLabel("  姓名 :");
    private JLabel jLabel2 = new JLabel("  学号 :");
    private JLabel jLabel3 = new JLabel("登录名 :");

    private String user;
    private JFrame frame;

    private Profile() {
        // 改变背景图片
        User user = getProfile();
        if(user != null){
            s1 = user.getName();
            s2 = user.getSid();
            s3 = user.getUsername();
            jLabel1.setText("  姓名 :"+s1);
            jLabel2.setText("  学号 :"+s2);
            jLabel3.setText("登录名 :"+s3);
        }
        Icon i = new ImageIcon(Profile.class.getResource("img/8.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);
        // 面板布局为空
        jPanel2.setLayout(null);
        // 标签

        jLabel.setFont(font);
        jLabel.setBounds(460, 50, 800, 70);
        jLabel.setForeground(Color.white);
        jLabel1.setBounds(500,230,400,50);
        jLabel1.setForeground(Color.white);
        jLabel1.setFont(font2);
        jLabel2.setBounds(500,330,400,50);
        jLabel2.setForeground(Color.white);
        jLabel2.setFont(font2);
        jLabel3.setBounds(500,430,400,50);
        jLabel3.setForeground(Color.white);
        jLabel3.setFont(font2);
        // 按钮
        button.setFont(font1);
        button2.setFont(font1);
        button3.setFont(font1);

        button.setBounds(150, 250, 250, 50);
        button2.setBounds(150, 310, 250, 50);
        button3.setBounds(150, 370, 250, 50);


        button.setBackground(Color.white);
        button2.setBackground(Color.white);
        button3.setBackground(Color.white);


        button.setForeground(Color.white);
        button2.setForeground(Color.white);
        button3.setForeground(Color.white);


        button.setOpaque(false);
        button2.setOpaque(false);
        button3.setOpaque(false);


        jPanel2.add(button);
        jPanel2.add(button2);
        jPanel2.add(button3);

        jPanel2.add(jLabel);
        jPanel2.add(jLabel1);
        jPanel2.add(jLabel2);
        jPanel2.add(jLabel3);
        jPanel2.add(Label);
//        jLayeredPane.add(button);
//        jLayeredPane.add(button2);
//        jLayeredPane.add(button3);
//
//        jLayeredPane.add(jLabel);
//        jLayeredPane.add(jLabel1);
//        jLayeredPane.add(jLabel2);
//        jLayeredPane.add(jLabel3);
//        jLayeredPane.add(Label);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserInterface.getInstance().user.setVisible(false);
                EditProfile.getInstance().setVisible(true);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserInterface.getInstance().user.setVisible(false);
                BorrowRecords.removeInstance();
                BorrowRecords.getInstance().setVisible(true);
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserInterface.getInstance().user.dispose();
                UserInterface.removeInstance();
                Profile.removeInstance();
                Search.removeInstance();
                BorrowAndReturn.removeInstance();
                EditProfile.removeInstance();
                BorrowRecords.removeInstance();
                Login.getInstance().setVisible(true);
                Login.loginUser = "";
            }
        });

        button.setFocusPainted(false);
        button2.setFocusPainted(false);
        button3.setFocusPainted(false);
    }
    public static Profile getInstance() {
        if(instance==null) instance=new Profile();
        return instance;
    }

    /**
     * @author zj
     * @return 获取登录用户用户信息
     */
    public static User getProfile(){
        ResultSet rs;
        String loginUser = Login.loginUser;
        Connection con = DatabaseConnection.getInstance().getCon();
        String sql = "select * from user where username = ?";
        PreparedStatement preSql;
        User user = null;
        try{
            preSql = con.prepareStatement(sql);
            preSql.setString(1, loginUser);
            rs = preSql.executeQuery();
            if(rs.next()){
                String name = rs.getString("name");
                String sid = rs.getString("sid");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Boolean isClosed = rs.getBoolean("isClosed");
                user = new User(name, sid, username, password, isClosed);
            }
        }
        catch (Exception e){
            System.out.println(e);
        }
        return user;
    }
    public static void removeInstance(){
        instance = null;
    }
}
